<template>
    <div>
        <el-dialog :destroy-on-close="true"
                   :title="isAddPage?'添加组织':'修改组织'"
                   :close-on-click-modal="false"
                   @close="closeDialog"
                   :visible="visible"
                   width="400px">
            <el-form ref="formRef" :model="form" :rules="rules"  label-width="120px" >

                        <el-row v-if="isAddPage">
                            <el-col :span="24">
                                <el-form-item label="一级组织"  prop="isOnRoot">
                                    <el-checkbox v-if="form.pid===0" v-model="form.isOnRoot" disabled></el-checkbox>
                                    <el-checkbox v-if="form.pid!==0" v-model="form.isOnRoot"></el-checkbox>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row v-if="isAddPage">
                            <el-col :span="24" v-if="!form.isOnRoot" >
                                <el-form-item label="上级组织"  prop="pName">
                                    <el-input v-model="form.pName" disabled :readonly="true"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="组织名称"  prop="orgName">
                                    <el-input v-model="form.orgName"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="组织编码"  prop="orgNo">
                                    <el-input v-model="form.orgNo"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>

                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="是否启用"  prop="isEnable">
                                    <el-checkbox v-model="form.isEnable"></el-checkbox>
                                </el-form-item>
                            </el-col>
                        </el-row>

                <el-row style="text-align: right">
                    <el-form-item>
                        <el-button type="primary" @click="submitForm()">保存</el-button>
                        <el-button @click="closeDialog" >取消</el-button>
                    </el-form-item>
                </el-row>

            </el-form>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        components: {
        },
        props: ['visible'],
        data() {
            return {
                isAddPage:true,
                currentRow:null,
                rowIndex:null,
                form: {
                    isEnable:true,
                    isOnRoot:false,
                },
                rules: {
                    orgName: [
                        { required: true, message: '请输入数据', trigger: 'blur' },
                        { min: 2, max: 64, message: '长度在 2 到 64 个字符', trigger: 'blur' }
                    ],
                    orgNo: [
                        { required: true, message: '请输入数据', trigger: 'blur' },
                        { min: 2, max: 64, message: '长度在 2 到 64 个字符', trigger: 'blur' }
                    ],
                }
            }
        },methods: {
            closeDialog() {
                this.$emit('update:visible',false);
                //Object.assign(this.$data, this.$options.data());
            },
            submitForm(){
                let _this=this;
                let url='/org/save';
                if (!_this.isAddPage){
                    url='/org/update';
                }
                this.$utils.submitFormData(_this,_this.$refs.formRef,url,_this.form,function (valid) {
                    return valid;
                },function () {
                    _this.closeDialog();
                    _this.$emit('loadData', true);
                });
            },

            initData(row){
                this.isAddPage=true;
                let pid=0;
                let pName=null;
                let isOnRoot=true;
                if (null!==row){
                    pid=row.id;
                    pName=row.orgName;
                    isOnRoot=false;
                }
                this.form={
                    pid:pid,
                    pName:pName,
                    isOnRoot:isOnRoot,
                    isEnable:true,
                }
            },
            loadData(row){
                let _this=this;
                this.isAddPage=false;
                setTimeout(function(){
                    _this.$http.post('/org/getOrg/'+row.id).then(function(res){
                        if(res.data.success){
                            _this.form=res.data.data;
                        }else{
                            _this.$message.error('操作失败！错误编码:'+res.data.code+'错误原因:'+res.data.msg);
                        }
                    },function(e){
                        _this.$message.error('操作失败！'+e);
                    });
                }, 100);
            },

        }
    }

</script>
